package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Created by Administrator on 2016.9.5.
 */
public class Test1627 extends JFrame{
    int number=0;
    public Test1627(){
        add(new Click());
    }

    public static void main(String[] args){
        JFrame frame=new Test1627();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(400,80);
        frame.setSize(600,600);
        frame.setTitle("手眼一体");
        frame.setVisible(true);
    }

    class Click extends JPanel{
        long time;
        int x;
        int y;

        public Click(){
            x=(int)(Math.random()*500);
            y=(int)(Math.random()*500);
            time=System.currentTimeMillis();

            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    super.mousePressed(e);

                    int x=e.getX();
                    int y=e.getY();

                    double distance=Math.pow(Click.this.x-x,2)
                            +Math.pow(Click.this.y-y,2);
                    if(distance<100){
                        number++;
                        Click.this.x=(int)(Math.random()*500);
                        Click.this.y=(int)(Math.random()*500);
                        repaint();
                    }
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if(number>=20){
                g.drawString("20次点击使用时间为："+(System.currentTimeMillis()-time)+"ms"
                        ,getWidth()/2,getHeight()/2);
            }else {
                g.fillOval(x,y,10,10);
            }
        }
    }
}
